package com.dh.cheesestrip.Util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Iterator;
import java.util.Stack;

public class ActUtil {
    private static Stack<Activity> mActivityStack;
    private static ActUtil mActUtil;

    private ActUtil() {
    }

    /**
     * 单一实例
     */
    public static ActUtil getInstance() {
        if (mActUtil == null) {
            mActUtil = new ActUtil();
        }
        return mActUtil;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<Activity>();
        }
        mActivityStack.add(activity);
    }

    /**
     * 获取栈顶Activity（堆栈中最后一个压入的）
     */
    public Activity getTopActivity() {
        Activity activity = mActivityStack.lastElement();
        return activity;
    }

    /**
     * 结束栈顶Activity（堆栈中最后一个压入的）
     */
    public void killTopActivity() {
        Activity activity = mActivityStack.lastElement();
        killActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void killActivity(Activity activity) {
        for (Iterator<Activity> it = mActivityStack.iterator(); it.hasNext(); ) {
            Activity act = it.next();
            if (act == activity) {
                act.finish();
                it.remove();
            }
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void killActivity(Class<?> cls) {
        for (Iterator<Activity> it = mActivityStack.iterator(); it.hasNext(); ) {
            Activity activity = it.next();
            Class<?> tempClass = activity.getClass();
            if (tempClass.equals(cls)) {
                activity.finish();
                it.remove();
            }
        }
    }


    /**
     * 结束所有Activity
     */
    public void killAllActivity() {

        for (Iterator<Activity> it = mActivityStack.iterator(); it.hasNext(); ) {
            Activity activity = it.next();
            activity.finish();
            it.remove();
        }
        mActivityStack.clear();
    }

    /**
     * 从栈中清除
     * @param activity
     */
    public void clearAct(Activity activity) {

        if(null==mActivityStack){
            return;
        }

        for (Iterator<Activity> it = mActivityStack.iterator(); it.hasNext(); ) {
            Activity act = it.next();
            if (act == activity) {
                act.finish();
                it.remove();
            }
        }

    }


    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            killAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
        }
    }

    /*
    双击两次返回
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (AndroidApplication.getInstance().getActivities().size() == 1) {
				if (AndroidApplication.getInstance().backKeyPressed) {
					Log.v("a.test", "final press");
					AndroidApplication.getInstance().exit();
				} else {
					Log.v("a.test", "first press");
					AndroidApplication.getInstance().backKeyPressed = true;
					AndroidApplication.getInstance().excuteExitTimerTask();
					toast("再按一次退出");
				}
			} else {
				Log.i("a.test", "size ="
						+ AndroidApplication.getInstance().getActivities()
								.size());
				// AndroidApplication.act_account--;
				finish();
			}
		}
		return false;
	}



     */

}
